package com.swu.education.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.text.SimpleDateFormat;


import com.swu.education.entity.Chat;
import com.swu.education.entity.ThumbsAndCommentRecord;
import com.swu.education.entity.User;

import com.swu.education.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.swu.education.repository.ChatRepository;
import com.swu.education.repository.ThumbsAndCommentRecordRepository;
import com.swu.education.repository.UserRepository;

import javax.servlet.http.HttpSession;

/**
 * @createdBy vivi
 *
 */
@RestController
public class ChatController {

	@Autowired
	ChatRepository chatRepository;
	@Autowired
	UserRepository userRepository;
	@Autowired
	ThumbsAndCommentRecordRepository recordRepository;

	@Autowired
	UserService userService;
	
	/**
	 * 验证用户名（ID/学号）、密码
	 * @param studentNumber
	 * @param password
	 * @return
	 */
	@RequestMapping(value="/checkLogin")
	public User checkUserLogin(@RequestParam(value="studentNumber") String studentNumber,
							   @RequestParam(value="password") String password,
							   HttpSession session){

		System.out.println("userId: "+studentNumber);
		System.out.println("password: "+password);

		User user = new User();
		user = userRepository.findUserByStudentNumberAndPassword(studentNumber,password);
		System.out.println(user);
		if(user == null){
			user.setRealname("sorry");
		}
		else{
			session.setAttribute("user", user);
			return user;
		}
		return user;
	}
	
	@RequestMapping(value="/getUser")
	public User getUserOfUserId(@RequestParam(value="userId") Integer userId){		
	
		User user = new User();
		user=userRepository.findOne(userId);
		return user;
		
	}
	@RequestMapping(value = "/getStudentByCourseId")
	public List<User> getStudentByCourseId(@RequestParam(value = "courseId") int courseId){
		System.out.println("courseID: "+courseId);
		return userService.getStuByCourseId(courseId);
	}
	
	/**
	 * 存储当前发送的消息
	 * @param userId
	 * @param chatContent
	 * @param type
	 * @return
	 */
	@RequestMapping(value="/groupChat")
	public Chat saveChatContent(@RequestParam(value="userId") Integer userId,
								@RequestParam(value="chatContent") String chatContent,
								@RequestParam(value="type") String type){
		
		Date now = new Date(); 
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");//可以方便地修改日期格式

		Chat chat= new Chat();
		chat.setType(type);
		chat.setChatRoomId(1);		//后续用来传输获取修改值
		chat.setChatContent(chatContent);
		chat.setGroupId(1);
		chat.setChatTime(dateFormat.format(now));//获取当前时间
		chat.setOwner(userId);
		
		chatRepository.save(chat);//将该条聊天记录存入数据库
		
		return chat;
	  
	}
	
	/**
	 * 获取当前聊天室的所有消息
	 * @param chatRoomId
	 * @return
	 */
	@RequestMapping(value="/getMessages")
	public List<Chat> getAllMessagesOfChatRoom(@RequestParam(value="chatRoomId") Integer chatRoomId){
		
		List<Chat> chats = new ArrayList<Chat>();
		chats = chatRepository.findByChatRoomId(chatRoomId);
	
		return chats;
	  
	}
	
	
	/**
	 * 该消息的点赞数增加1
	 * @param chatId
	 * @return
	 */
	@RequestMapping(value="/thumbsUp")
	public Chat saveThumbsUpNum(@RequestParam(value="chatId") Integer chatId){
		
		Chat chat= new Chat();
		chat = chatRepository.findOne(chatId);
		chat.setLikeNum(chat.getLikeNum()+1);
		chatRepository.save(chat);//将该条聊天记录存入数据库
		
		return chat;
	  
	}
	
	/**
	 * 创建当前聊天室所有消息的点赞和评论记录，该记录用于存储每个人对每一条评论 是否点赞 && 是否评论 && 评论内容
	 * @param chatId
	 * @param userId
	 * @return
	 */
	@RequestMapping(value="/createRecord")
	public List<ThumbsAndCommentRecord> createThumbsAndAskRecord(@RequestParam(value="chatId") Integer chatId,
																 @RequestParam(value="userId") Integer userId){
		User user1 = new User();
		
		//获取当前userId对应的user
		user1 = userRepository.findOne(userId);
		
		//获取当前user所在group的所有users
		List<User> users = new ArrayList<User>();
		users = userRepository.findByGroupId(user1.getGroupId());
		
		List<ThumbsAndCommentRecord> records = new ArrayList<ThumbsAndCommentRecord>(5);
		
		for(int i = 0;i < users.size(); i ++){
				
				ThumbsAndCommentRecord record1 = new ThumbsAndCommentRecord();
				record1.setChatId(chatId);
				record1.setUserId(users.get(i).getId());
			    record1.setChatRoomId(1);
			    
			    recordRepository.save(record1); //存入数据库
			    
			    records.add(record1); //添加至List中，用于返回前台
			}
			
		return records;
	  
	}

	/**
	 * 存储当前user对当前chat已点赞
	 * @param chatId
	 * @param userId
	 * @return
	 */
	@RequestMapping(value="/saveThumbs")
	public ThumbsAndCommentRecord saveThumbsRecord(@RequestParam(value="chatId") Integer chatId,
													@RequestParam(value="userId") Integer userId){
		
		ThumbsAndCommentRecord record = new ThumbsAndCommentRecord();
		record = recordRepository.findByChatIdAndUserId(chatId, userId);
		record.setThumbsUp(true);
		recordRepository.save(record);
		return record;
	  
	}
	
	
	/**
	 * 存储当前user对当前chat已评论，同时存储评论内容
	 * @param chatId
	 * @param userId
	 * @param commentContent
	 * @return
	 */
	@RequestMapping(value="/saveComment")
	public ThumbsAndCommentRecord saveCommentRecord(@RequestParam(value="chatId") Integer chatId,
													@RequestParam(value="userId") Integer userId,
													@RequestParam(value="commentContent") String commentContent){
		
		System.out.println("----------"+chatId+"  "+userId+"  "+commentContent+"------------");
		ThumbsAndCommentRecord record = new ThumbsAndCommentRecord();
		record = recordRepository.findByChatIdAndUserId(chatId, userId);
		record.setAsked(true);
		record.setCommentContent(commentContent);
		recordRepository.save(record);
		return record;
	  
	}
	
	/**
	 * 获取当前chat的所有点赞评论记录
	 * @param chatId
	 * @return
	 */
	@RequestMapping(value="/getComments")
	public List<ThumbsAndCommentRecord> getComments(@RequestParam(value="chatId") Integer chatId){
		
		List<ThumbsAndCommentRecord> records = new ArrayList<ThumbsAndCommentRecord>(5);
		records = recordRepository.findByChatId(chatId);
		return records;
	  
	}
	
	/**
	 * 
	 * @param chatId
	 * @param userId
	 * @return
	 */
	@RequestMapping(value="/getRecord")
	public ThumbsAndCommentRecord getThumbsAndCommentRecord(@RequestParam(value="chatId") Integer chatId,
									@RequestParam(value="userId") Integer userId){
		
		ThumbsAndCommentRecord record = new ThumbsAndCommentRecord();
		record = recordRepository.findByChatIdAndUserId(chatId, userId);
		
		/*！！！！无法在此处直接进行数据库中boolean类型的判断，原因：未知？？？*/
		
		return record;
	  
	}
/*	页面跳转不成功
	@RequestMapping(value="/group")
	public String groupChat(@RequestParam(value="User") Integer id){
			System.out.println(id+"aaaa");
			return "chatting";
	}*/
	

}
